<template>
	<view class="account-content">
		<view class="user-header">
			<u-avatar :src="avater" :size="180" @click="taggleAvater"></u-avatar>
			<view @click="taggleAvater">点击修改头像</view>
		</view>
		<view class="user-info">
			<view class="info-item">
				<view>ID</view>
				<view class="item-content">{{ID}}</view>
			</view>
			<view class="info-item">
				<view>账户身份</view>
				<view class="item-content">{{is_teacher == 0?'学员':'讲师'}}</view>
			</view>
			<view class="info-item" @click="$refs.inputDialog.open()">
				<view>账号昵称</view>
				<view class="item-content">{{nickname}}<u-icon name="arrow-right" color="#999"></u-icon></view>
			</view>
			<view class="phone-box">
				<view>手机号码</view>
				<view class="item-content">
					<view>{{phone}}</view>
					<!-- <u-button open-type='getPhoneNumber' @getphonenumber="getphonenumber" shape="circle" size="mini" :custom-style="customStyle">一键获取</u-button> -->
				</view>
			</view>
		</view>
		<!--  -->
		<!-- 输入框示例 -->
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose"  mode="input" title="输入要修改的昵称" :value="nickname"
					placeholder="请输入内容" @confirm="nickNameConfirm" confirmText="确认修改"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import { getUserInfo,changeAvaters,uploadImage,editNickname } from '@/api/user.js'
import { toast } from '../../utils/common';
	import config from '@/config/config.js'
	export default {
		data() {
			return {
				phone:'13314411111',
				customStyle:{
					color:'#fff',
					background:'#2AC092'
				},
				avater:'',
				ID:'',
				is_teacher:0,
				nickname:''
			}
		},
		mounted() {
			this.getUserInfos()
		},
		methods:{
			getphonenumber(e){
				console.log(e);
			},
			getUserInfos(){
				let that = this;
				getUserInfo().then(res=>{
					// console.log(8855,res.data.data);
					that.phone = res.data.data.phone;
					that.ID = res.data.data.id;
					that.avater = res.data.data.full_avatar;
					that.is_teacher = res.data.data.is_teacher
					that.nickname = res.data.data.nickname
					that.$store.commit('userInfo',res.data.data)
				}).catch()
			},
			// 修改昵称
			nickNameConfirm(e){
				let that = this
				editNickname({nickname:e}).then(res=>{
					toast('修改成功')
					that.getUserInfos()	
				})
			},
			// 修改头像
			taggleAvater(){
				let that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'],   //album 从相册选图，camera 使用相机
					success: function (res) {
						uni.showLoading({
							title: '正在上传',
							mask: true
						});
						console.log(res.tempFilePaths);
						let uploadTask = uni.uploadFile({
							url: config.baseUrl + '/api/public/images/upload',
							filePath: res.tempFilePaths[0],
							name: 'file',
							header: {
							            'content-type': 'multipart/form-data' 
							        },
							success: (res8) => {
								changeAvaters({avatar:JSON.parse(res8.data).data}).then(res=>{
									toast('修改成功');
									that.getUserInfos()
								})
							},
							complete(res) {
								// console.log(res);
								uni.hideLoading();
							}
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.account-content {
		padding-top: 20rpx;
	}

	.user-header {
		padding: 20rpx;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;

		>view {
			margin-top: 30rpx;
		}
	}
	
	.user-info{
		margin-top: 20rpx;
		padding: 0 20rpx;
		background-color: #fff;
		
		.item-content{
			color: #999999;
		}
		.info-item{
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;
			border-bottom: 1px solid #DFE0E6;
			.changeName{
				font-size: 22upx;
				color: #DFE0E6;
			}
		}
		
		.phone-box{
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;
			
			>view:nth-child(2){
				display: flex;
				align-items: center;
				
				>view{
					margin-right: 20rpx;
				}
			}
		}
	}
</style>